{VERSION 6 0 "IBM INTEL NT" "6.0" } {USTYLETAB {CSTYLE "Maple Input" -1 0 "Courier" 0 1 255 0 0 1 0 1 0 0 1 0 0 0 0 1 }{CSTYLE "2D Math" -1 2 "Times" 0 1 0 0 0 0 0 0 2 0 0 0 0 0 0 1 }{CSTYLE "2D Comment" 2 18 "" 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 } {CSTYLE "" -1 256 "" 1 18 0 0 0 0 0 0 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 257 "" 0 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 258 "" 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 }{CSTYLE "" -1 259 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 }{CSTYLE "" -1 260 "" 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 } {CSTYLE "" -1 261 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 }{CSTYLE "" -1 262 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 }{CSTYLE "" -1 263 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 1 }{CSTYLE "" -1 264 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "_cstyle4" -1 265 "Times" 1 12 0 0 0 1 2 2 2 2 2 2 0 0 0 1 }{CSTYLE "_cstyle5" -1 266 "Times" 1 12 0 0 0 1 2 1 2 2 2 2 0 0 0 1 }{CSTYLE "" -1 267 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 268 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 269 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 270 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 273 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 } {CSTYLE "" -1 274 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 275 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 276 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 277 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 278 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 } {CSTYLE "" -1 279 "" 0 1 0 0 0 0 0 1 1 0 0 0 0 0 0 0 }{CSTYLE "" -1 280 "" 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 }{CSTYLE "" -1 281 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 282 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "_cstyle2" -1 283 "Times" 1 18 0 0 0 1 2 1 2 2 2 2 0 0 0 1 }{CSTYLE "" -1 284 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE " " -1 285 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 286 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "_cstyle6" -1 287 "Courier" 1 12 255 0 0 1 2 1 2 2 1 2 0 0 0 1 }{CSTYLE "" -1 288 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 289 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 } {CSTYLE "" -1 290 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 291 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 292 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 293 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 294 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 } {CSTYLE "" -1 295 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 296 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 297 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 298 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 299 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 } {CSTYLE "" -1 300 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 301 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 302 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 303 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 304 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 } {CSTYLE "" -1 305 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "_cstyle 7" -1 306 "Times" 1 12 0 0 0 1 1 2 2 2 2 2 0 0 0 1 }{CSTYLE "_cstyle8 " -1 307 "Times" 1 12 0 0 0 1 2 2 1 2 2 2 0 0 0 1 }{CSTYLE "_cstyle9" -1 308 "Times" 1 12 0 0 0 1 2 1 1 2 2 2 0 0 0 1 }{CSTYLE "" 265 309 " " 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 310 "" 0 1 0 0 0 0 0 1 0 0 0 0 0 0 0 0 }{CSTYLE "" -1 311 "" 0 1 0 0 0 0 0 1 1 0 0 0 0 0 0 0 }{CSTYLE "" -1 312 "" 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 }{CSTYLE "_cst yle10" -1 313 "Times" 1 12 0 0 0 1 2 2 1 2 2 2 0 0 0 1 }{PSTYLE "Norma l" -1 0 1 {CSTYLE "" -1 -1 "Times" 1 12 0 0 0 1 2 2 2 2 2 2 1 1 1 1 } 1 1 0 0 0 0 1 0 1 0 2 2 0 1 }{PSTYLE "Heading 1" -1 3 1 {CSTYLE "" -1 -1 "Times" 1 18 0 0 0 1 2 1 2 2 2 2 1 1 1 1 }1 1 0 0 8 4 1 0 1 0 2 2 0 1 }{PSTYLE "Normal" -1 256 1 {CSTYLE "" -1 -1 "Times" 1 12 0 0 0 1 2 1 2 2 2 2 1 1 1 1 }3 1 0 0 0 0 1 0 1 0 2 2 0 1 }} {SECT 0 {EXCHG {PARA 256 "" 0 "" {TEXT -1 0 "" }{TEXT 256 9 "Lecture 2 " }}}{SECT 1 {PARA 3 "" 0 "" {TEXT -1 15 "Shift + Return " }{TEXT 257 2 "vs" }{TEXT -1 11 " the Return" }}{PARA 0 "" 0 "" {TEXT 258 66 "If t he cursor is on the same line as a Maple command, hitting the " } {TEXT 259 6 "Return" }{TEXT 260 52 " key will cause the command to be \+ executed. Hitting " }{TEXT 261 14 "Shift + Return" }{TEXT -1 16 " (hol d down the " }{TEXT 262 5 "Shift" }{TEXT -1 17 " key and then hit" } {TEXT 263 7 " Return" }{TEXT -1 107 ") will move the cursor to the nex t line without executing the command and without introducing a new pro mpt " }{TEXT 264 1 ">" }{TEXT -1 60 ". This is useful in making you c ode more readable. Also, s" }{TEXT 265 233 "ometimes you don't want M aple to execute a sequence of commands till you have typed in several \+ on different lines. To do this one needs to know how to go to another \+ output line without getting a new prompt, that is, how to suppress " }{TEXT 266 5 ">. " }{TEXT 265 21 "In such cases use of " }{TEXT 266 14 "Shift + Return" }{TEXT 265 16 " (hold down the " }{TEXT 266 5 "Shi ft" }{TEXT 265 12 " key and hit" }{TEXT 266 7 " Return" }{TEXT 265 23 ") will accomplish this." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 " " 0 "" {TEXT -1 154 "Before continuing, let's practice. Reproduce the following lines of code exactly as they appear here. Don't worry if \+ you don't understand what it means." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 45 "for i from 1 to 3 do\n print(Hello!);\nend do;" }}} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}{PARA 0 "" 0 "" {TEXT -1 35 "To get the indentation on the line " }{MPLTEXT 1 0 15 "print(\" Hello\");" }{TEXT -1 63 " you will have to use the space bar to insert the extra spaces." }}{PARA 0 "" 0 "" {TEXT -1 39 "Note that you do no t have to press the " }{TEXT 267 14 "Shift + Return" }{TEXT -1 150 " i n order to execute the code. This is a convention we use only to make the code more readable for us. Notice that the same code works in th is form:" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 43 "for i from 1 to \+ 3 do print(Hello!); end do;" }}}{PARA 0 "" 0 "" {TEXT -1 48 "Let's mov e on to some basic programing concepts." }}}{SECT 1 {PARA 3 "" 0 "" {TEXT -1 18 "If...then...end if" }}{PARA 0 "" 0 "" {TEXT -1 434 "Compu ter programs that you write aren't able to think for themselves (if yo u can write a working AI, then you shouldn't be in this class and I ap ologize for boring you so much). When people say, \"This computer nev er does what I tell it to!\" they cannot be further from the truth. C omputer programs will only do what you tell them to. If it does not d o what it's supposed to do, then you, the programmer, wrote the code i ncorrectly." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 102 "So how does a computer program work. In other languages, when you write a program, you will begin by " }{TEXT 268 26 "dimensionaliz ing variables" }{TEXT -1 468 ". In other words, you might tell the pr ogram that \"x\" will be an integer, \"y\" is a floating point decimal , and \"s\" is a \"string\", or a sequence of letters. If you don't t ell the program what the variables are, and then you try to use them i n the program, you risk crashing. Programming in Maple is similar, al though Maple is somewhat more adaptive than other programming language s. When we get into more complicated procedures, you will see there h ow to dimension " }{TEXT 269 5 "local" }{TEXT -1 107 " variables. For now, let's just look at what to do with the variables assuming that w e know what they are." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 451 "Having the variables is nice. You've told the comput er to set aside some of its precious memory to make room for your unkn owns. Then the computer sits there, waiting (almost a lifetime, in co mputer terms), for you to tell it what to do with these variables. A \+ computer program must accurately execute commands and operations on th e variables in order to function properly (DUH!!). So what are the co mmands, and how does the computer evaluate them?" }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 220 "The most basic function \+ computers have is to evaluate whether or not something is true. This \+ is, in part, why computer code is written in binary (1's and 0's). On e way we can tell a computer to evaluate truth is by an " }{TEXT 270 10 " If...Then" }{TEXT -1 98 " statement. In this case, we are tellin g the computer to determine if some condition is met, and " }{TEXT 273 2 "if" }{TEXT -1 5 " so, " }{TEXT 274 4 "then" }{TEXT -1 48 " exec ute some command. Here's a simple example." }}{EXCHG {PARA 0 "> " 0 " " {MPLTEXT 1 0 5 "x:=5;" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 67 " if type(x,integer)=true then\n print(\"This is an integer\");\nend if ;" }}}{EXCHG }{PARA 0 "" 0 "" {TEXT -1 36 "Here we used the built-in p rocedure " }{TEXT 275 4 "type" }{TEXT -1 93 ", which evaluates the Boo lean (true or false) statement \"x is an integer\". For more help on \+ " }{TEXT 276 4 "type" }{TEXT -1 19 ", use the help page" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 5 "?type" }}}{PARA 0 "" 0 "" {TEXT -1 0 "" }{TEXT 282 15 "Important NOTE:" }{TEXT -1 50 " Look at the end of the code above. It ends with " }{MPLTEXT 1 0 7 "end if;" }{TEXT -1 70 ". If you do not put this line in, then you will get an error mess age." }}{PARA 0 "" 0 "" {TEXT -1 99 "Now let's change things a bit.. \+ Let's make x something other than an integer and see what happens." }} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 7 "x:=3.5;" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 67 "if type(x,integer)=true then\n print(\"This \+ is an integer\");\nend if;" }}}{EXCHG }{PARA 0 "" 0 "" {TEXT -1 52 "No tice that nothing happened when you executed this " }{TEXT 277 9 "If.. .Then" }{TEXT -1 189 " statement. Why? the variable x is not an integ er, and you only told it to print something if x was an integer. Ther e's a simple way to rectify this problem which we will look at later. \+ " }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 111 "Let 's look at something which will seem obvious, but is a common mistake. Why doesn't the following code work?" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 5 "x:=4;" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 67 "if type(y,integer)=true then\n print(\"This is an integer\");\nend if; " }}}{PARA 0 "" 0 "" {TEXT -1 19 "What's the point? " }{TEXT 278 0 " " }{TEXT 279 50 "Make sure you a manipulating the correct varaibles" } {TEXT 280 0 "" }{TEXT -1 162 ". Here, the program does not know what \+ \"y\" is because we haven't created a variable \"y\". We make \"x\" t o be an integer, but we told the computer to evaluate \"y\"." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 108 "Enough with th is for now. Let's move on to something else, and then we'll come back and study some more on " }{TEXT 281 9 "If...Then" }{TEXT -1 12 " stat ements." }}}{SECT 1 {PARA 3 "" 0 "" {TEXT 283 25 "For . . . do . . . e nd do" }{TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 39 "In the previous s ection we saw that an " }{TEXT 284 9 "If...Then" }{TEXT -1 215 " evalu ated the truth of a statement and then acted if truth was established. Here we'll look at a type of code that will continually execute itse lf repeatedly while some particular condition is met. It's called a \+ " }{TEXT 285 4 "Loop" }{TEXT -1 29 ", and you create loops using " } {TEXT 286 17 "For...do...end do" }{TEXT -1 125 ". We saw one of these at the beginning of this lecture, in which we printed Hello three tim es. Consider the following code:" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 46 "for i from 1 to 4 do \n i,i^2,i^3,i^4;\nend do;" }}} {PARA 0 "" 0 "" {TEXT -1 8 "What is " }{MPLTEXT 1 0 1 "i" }{TEXT -1 39 " in this code? This is what we call a " }{TEXT 288 7 "counter" } {TEXT -1 103 ". It is a variable that we will iterate to establish ho w long a loop must be executed. In this case, " }{MPLTEXT 1 0 1 "i" } {TEXT -1 112 " was initialized to the value of 1. Then the program ev aluated 1, 1^2, 1^3, and 1^4. When it reached the line " }{MPLTEXT 1 0 7 "end do;" }{TEXT -1 49 ", it looped back to the first line and inc reased " }{MPLTEXT 1 0 1 "i" }{TEXT -1 34 " by 1. It then evaluated w hether " }{MPLTEXT 1 0 1 "i" }{TEXT -1 105 " was greater than 4, and s eeing that it was not, executed the loop again. It continued doing th is until " }{MPLTEXT 1 0 1 "i" }{TEXT -1 94 " reached 4. At this poin t, it ran through the loop, went back to the first line, incremented \+ " }{MPLTEXT 1 0 1 "i" }{TEXT -1 11 ", saw that " }{MPLTEXT 1 0 1 "i" } {TEXT -1 63 " was greater than 4, and ended the loop. Notice the foll owing:" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 2 "i;" }}}{EXCHG } {PARA 0 "" 0 "" {TEXT -1 168 "Note that counters do not have to count \+ by 1, nor do they have to be positive or even be increasing. They mus t be numbers, that's it. Consider the following examples." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 58 "for x from -2 to 2 by 2 do\n x, ev alf[15](sin(x));\nend do;" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 44 "for j from 10 to 0 by -2 do\n j,j^2;\nend do;" }}}{EXCHG }{PARA 0 "" 0 "" {TEXT 265 296 "Now we illustrate a common technique for addi ng a specified number of terms. There is nothing special about SUM as \+ a variable. We could use any (unprotected) name for this variable (the word \"sum\" is protected, so we cannot use it as a variable). We wil l find the sum of the integers 1 through 10" }{TEXT -1 106 ". We begi n by setting the variable SUM equal to 0. If we do not do this, then \+ the program will not work." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 49 "SUM:=0;\nfor i from 1 to 10 do\nSUM:=SUM+i;\nend do;" }}}{EXCHG } {PARA 0 "" 0 "" {TEXT -1 64 "Again, note the value of the counter once the loop has executed." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 2 "i; " }}}{PARA 0 "" 0 "" {TEXT -1 49 "See the value of SUM after the loop \+ has executed:" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 4 "SUM;" }}} {PARA 0 "" 0 "" {TEXT -1 81 "To remove all of the unnecessary output, \+ we can insert a colon at the end of the " }{TEXT 289 4 "Loop" }{TEXT -1 12 " as follows:" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 55 "SUM:= 0:\nfor i from 1 to 10 do\n SUM:=SUM+i;\nend do:\nSUM;" }}}{EXCHG } {PARA 0 "" 0 "" {TEXT 265 31 "For another example we compute " } {XPPEDIT 18 0 "Sum(1/k,k = 1 .. 100);" "6#-%$SumG6$*&\"\"\"F'%\"kG!\" \"/F(;F'\"$+\"" }{TEXT 265 33 " : This time we use the variable " } {TEXT 266 5 "total" }{TEXT 265 59 " to keep track of the sum. Of cours e, any variable would do" }{TEXT -1 1 "." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 68 "total:=0:\nfor k from 1 to 100 do \n total:=total+1/k ; \nend do:\ntotal:" }}}{PARA 0 "" 0 "" {TEXT -1 12 "To see what " } {TEXT 290 5 "total" }{TEXT -1 41 " is evaluated as a decimal, we use e valf." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 13 "evalf(total);" }}}} {SECT 1 {PARA 3 "" 0 "" {TEXT -1 33 "Combining If...Then with For...do " }}{PARA 0 "" 0 "" {TEXT -1 59 "Before we jump into this section, let 's first consider the " }{TEXT 291 3 "mod" }{TEXT -1 383 " function fr om number theory. When you were in elementary school learning long di vision, you learned that if one number does not divide evenly into ano ther, then the division will result in a remainder. For instance, 12 \+ divided by 5 will have a remainder of 2. 14 divided by 5 will have a \+ remainder of 4. 25 divided by 5 will have a remainder of 0, since 25 \+ is a multiple of 5. " }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 4 "The " }{TEXT 292 7 "modulus" }{TEXT -1 5 ", or " } {TEXT 293 3 "mod" }{TEXT -1 101 " function, relates a number to its re mainder when divided by another number. Here are some examples:" }} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 9 "12 mod 5;" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 11 "modp(14,5);" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 9 "25 mod 5;" }}}{EXCHG }{PARA 0 "" 0 "" {TEXT -1 13 "The refore is " }{TEXT 294 36 "n mod d = 0, then n is a factor of d" } {TEXT -1 121 ". Let's find all of the factors of 54. Notice how inde ntation is used here to identify where each part begins and ends." }} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 81 "n:=54:\nfor i from 1 to n do \n if n mod i = 0 then\n print(i);\n end if:\nend do;" }}}{EXCHG }{PARA 0 "" 0 "" {TEXT -1 133 "Feel free to change the value of n in t he above program to experiment. Note that the indentation is not nece ssary, but very helpful." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 71 " n:=54:for i from 1 to n do if n mod i = 0 then print(i); end if:end do ;" }}}{EXCHG }{PARA 0 "" 0 "" {TEXT -1 81 "Let's have Maple print out \+ the squares of all of the multiples of 4 from 0 to 50." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 77 "for i from 1 to 50 do\n if i mod 4 =0 then\n print(i,i^2);\n end if:\nend do;" }}}{EXCHG }{PARA 0 "" 0 "" {TEXT -1 26 "We've seen two example of " }{TEXT 295 9 "If...Then " }{TEXT -1 12 " statements " }{TEXT 296 6 "nested" }{TEXT -1 4 " in \+ " }{TEXT 297 8 "For...do" }{TEXT -1 50 " loops. The nesting can work \+ the other way around" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 84 "j:=1 2:\nif j mod 2=0 then\n for i from 2 to j by 2 do\n print(i);\n e nd do;\nend if;" }}}{EXCHG }{PARA 0 "" 0 "" {TEXT -1 29 "This example \+ took some value " }{TEXT 298 1 "j" }{TEXT -1 99 ", evaluated if it was even, and if so, then printed all of the even numbers up to it using \+ a loop. " }}}{SECT 1 {PARA 3 "" 0 "" {TEXT 283 42 "The set of positive divisors of an integer" }{TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 268 "We've seen examples of how to use loops to generate a bunch of nu mbers given some criteria. But how useful is this in reality. Can th e programs we've written so far manipulate the solutions we've generat ed? No! We never saved the solutions. We never constructed an " } {TEXT 299 6 "object" }{TEXT -1 120 " to store this information. Such \+ objects can be sets, lists, arrays, matrices, vectors, etc. Here we w il look at sets." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 89 "Hopefully, by this point of your mathematical career, you been exposed to the concept of " }{TEXT 300 4 "sets" }{TEXT -1 35 ". \+ If not, here's a brief overview:" }}{PARA 0 "" 0 "" {TEXT -1 2 "A " } {TEXT 301 3 "set" }{TEXT -1 15 " is a group of " }{TEXT 302 8 "element s" }{TEXT -1 67 ". No element in a set is repeated. Two sets can be \+ combined by a " }{TEXT 303 5 "union" }{TEXT -1 78 ". You can determin e what elements are common to two sets by looking at their " }{TEXT 304 13 "intersection." }{TEXT -1 58 " Sets are denoted by using \{\}. Here are some examples. " }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 108 "restart:\nS:=\{0,1,2,3,4,5,6,7,8,9,10,2,4,6,8\};\nT:=\{2,4,6,8,10 ,12,14,16,18,20\};\nU:=\{1,3,5,7,9,11,13,15,17,19\};" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 14 "V:= S union T;" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 17 "W:=T intersect U;" }}}{EXCHG }{PARA 0 "" 0 "" {TEXT -1 162 "Note that the set W contains no elements. We call it th e empty set. The elements 2,4,6,and 8 were repeated in the set S, but the output only displays them once." }}{PARA 0 "" 0 "" {TEXT -1 100 " Also, sets do not have contain only numbers. They can contain strings , functions, expressions, etc." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 34 "Z:=\{red, x^2 +5*x-126, y,2*x+3=0\};" }}}{EXCHG }{PARA 0 "" 0 " " {TEXT -1 11 "Maple uses " }{TEXT 305 10 "printlevel" }{TEXT -1 258 " to determine how much output to show you. Initially, printlevel =1. \+ This will display things as we have seen. If we increase printlevel, we will increase the output. This is useful for debugging a program, but will take up more space. Let's go back and" }{TEXT 265 21 " see \+ how to form the " }{TEXT 306 3 "set" }{TEXT 265 32 " of all positive d ivisors of 28." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 8 "restart:" } }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 14 "printlevel:=2;" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 97 "n:=28\nS:=\{\}:\nfor i from 1 to n \+ do\n if n mod i = 0 then\n S:= S union \{i\};\n end if;\nend do; \nS;" }}}{EXCHG }{EXCHG }{PARA 0 "" 0 "" {TEXT -1 47 "Now move the cur sor back to the line that says " }{MPLTEXT 1 0 14 "printlevel:=2;" } {TEXT -1 18 " and change it to " }{MPLTEXT 1 0 14 "printlevel:=1;" } {TEXT -1 73 ". Executing the code again only outputs the final elemen ts of the set S." }}{PARA 0 "" 0 "" {TEXT -1 0 "" }}{PARA 0 "" 0 "" {TEXT -1 210 "Note that we can keep using this code, but for different values of n we'll either have to re-type the program or erase our pre vious output. Let's convert this into a procedure so we do not have t his problem. " }{TEXT 265 28 "We will call this procedure " }{TEXT 266 8 "Divisors" }{TEXT 265 50 ". Examine this procedure closely. Note the use of " }{TEXT 309 7 "return " }{TEXT 265 63 "on the next to las t line of the definition of this procedure. " }{TEXT 307 11 "DO NOT U SE " }{TEXT 308 5 "print" }{TEXT 307 13 " in place of " }{TEXT 308 6 " return" }{TEXT 307 40 ". We will explain the difference later." }} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 127 "Divisors:= proc(n)\nS:=\{\} :\nfor i from 1 to n do\n if n mod i = 0 then\n S:= S union \{i\}; \n end if;\nend do;\nreturn S;\nend proc;" }}}{EXCHG }{PARA 0 "" 0 " " {TEXT -1 222 "Note the three lines of ouput here. The first two are Maple complaining about how we never told it what S and i are in the \+ procedure. The last line is the entire procedure written with no extr a spacing. Here's a remedy:" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 138 "Divisors:= proc(n)\nlocal S,i:\nS:=\{\}:\nfor i from 1 to n do\n \+ if n mod i = 0 then\n S:= S union \{i\};\n end if;\nend do;\nretu rn S;\nend proc:" }}}{PARA 0 "" 0 "" {TEXT -1 245 "Here I have aded a \+ line of code declaring the variables S and i local to the procedure. \+ This means that no other part of the program can manipulate these valu es outside of the procedure. We'll discuss this in more depth later. \+ Just remember, " }{TEXT 310 0 "" }{TEXT 311 40 "always declare the va riables to be local" }{TEXT 312 0 "" }{TEXT -1 155 ". Also, I did awa y with the last line of output by changing the semi-colon at the last \+ line of code into a colon. Now, let's play with our new procedure." } }{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 13 "Divisors(10);" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 14 "Divisors(163);" }}}{EXCHG {PARA 0 " > " 0 "" {MPLTEXT 1 0 14 "Divisors(146);" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 15 "Divisors(1463);" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 17 "Divisors(11258); " }}}{EXCHG }}{SECT 1 {PARA 3 "" 0 " " {TEXT -1 15 "More procedures" }}{PARA 0 "" 0 "" {TEXT 265 49 "Let's \+ look again at how to compute a sum such as " }{XPPEDIT 18 0 "Sum(i^2,i = 1 .. 4);" "6#-%$SumG6$*$%\"iG\"\"#/F';\"\"\"\"\"%" }{TEXT 265 95 " \+ . Let's try to make it general by introducing a variable n. We start \+ by setting it equal to 4" }{TEXT -1 1 "." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 71 "n:=4:\nTotal:=0:\nfor i from 1 to n do\n Total:=Tota l+i^2:\nend do:\nTotal;" }}}{PARA 0 "" 0 "" {TEXT -1 124 "If we wanted to use n=15, we could go back and make the necessary changes. However , it would be easier to write a procedure " }{TEXT 265 11 "to compute \+ " }{XPPEDIT 18 0 "Sum(i^2,i = 1 .. n);" "6#-%$SumG6$*$%\"iG\"\"#/F';\" \"\"%\"nG" }{TEXT 265 34 " for any given positive integer n" }{TEXT -1 31 ". Let's call this procedure F." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 120 "F:=proc(n)\n local i,Total;\n Total:=0:\n for i f rom 1 to n do\n Total:=Total+i^2:\n end do;\n return Total;\nend \+ proc:" }}}{PARA 0 "" 0 "" {TEXT -1 52 "Let's apply this procedure to n =4, n=15, and n=1000:" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 5 "F(4) ;" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 6 "F(15);" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 8 "F(1000);" }}}{PARA 0 "" 0 "" {TEXT -1 101 "The procedures we write can be given more than one variable. \+ For instance, here is a procedure that " }{TEXT 265 13 "will compute \+ " }{XPPEDIT 18 0 "sum(f(i),i = a .. b);" "6#-%$sumG6$-%\"fG6#%\"iG/F); %\"aG%\"bG" }{TEXT 265 85 " where a and b are any integers and f is a ny function. We will call the procedure S." }}{EXCHG {PARA 0 "> " 0 " " {MPLTEXT 1 0 116 "S:=proc(f,a,b)\nlocal i, Total;\nTotal:=0;\nfor i \+ from a to b do\n Total:=Total + f(i);\nend do;\nreturn Total;\nend pr oc:" }}}{PARA 0 "" 0 "" {TEXT -1 18 "Some applications:" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 11 "S(sin,1,5);" }}}{EXCHG {PARA 0 "> \+ " 0 "" {MPLTEXT 1 0 16 "S(x->x^2, 1, 6);" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 10 "S(f,-3,5);" }}}{EXCHG }{PARA 0 "" 0 "" {TEXT -1 64 "N ote that the order in which we call the procedure is important." }} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 11 "S(1,5,sin);" }}}{EXCHG } {PARA 0 "" 0 "" {TEXT 265 70 "Next we make a procedure we call H which has the property that H(n) = " }{XPPEDIT 18 0 "infinity;" "6#%)infini tyG" }{TEXT 265 48 " if n is greater than 100 and H(n) = 0 if not" }{TEXT -1 0 "" }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 71 "H:=proc(n) \nif n>100 then\n return infinity:\nend if:\nreturn 0:\nend proc:" }} }{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 7 "H(100);" }}}{EXCHG {PARA 0 " > " 0 "" {MPLTEXT 1 0 7 "H(101);" }}}{EXCHG }{PARA 0 "" 0 "" {TEXT -1 105 "Hopefully, you've seen the factorial function in your math classe s. If not, here's a simple description:" }}{PARA 0 "" 0 "" {TEXT -1 82 "For any positive integer n, we denote n factorial as n!. We defin e it as follows:" }}{PARA 0 "" 0 "" {TEXT -1 56 "n! = n(n-1)(n-2)...(3 )(2)(1). Also, 0!=1 by definition." }}{PARA 0 "" 0 "" {TEXT -1 73 "Le t's write a procedure that computes the factorial of a given number n. " }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 167 "Factorialproc:=proc(n) \nlocal Fac, i:\nFac:=1:\nif n=0 then \n Fac:=1 \nend if;\nif n>0 the n \n for i from 1 to n do\n Fac:=i*Fac:\n end do \nend if;\nretur n Fac;\nend proc:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 69 "Factor ialproc(0),Factorialproc(3),Factorialproc(5),Factorialproc(10);" }}}} {SECT 1 {PARA 3 "" 0 "" {TEXT -1 33 "Making a program into a procedure " }}{PARA 0 "" 0 "" {TEXT -1 518 "You might be wondering now, \"Do I a lways have to write a procedure?\". No! The examples we've seen show us that if we need to do a computation repeatedly, then a procedure w orks best. But if you only need to find one result for one problem an d don't need to repeat that code or call it later, then a program work s just fine. Below are two programs that we've already converted into procedures. Collapse the above sections, then try to convert these p rograms into procedures and evaluate them for the given numbers." }} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 72 "n:=4: \nTotal:=0:\nfor i fro m 1 to n do\n Total:=Total+i^2:\nend do:\nTotal;" }}}{EXCHG {PARA 0 " > " 0 "" {MPLTEXT 1 0 0 "" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 102 "n:=10:\nFactorialloop :=1:\nfor i from 1 to n do\n Factorialloop:=Factorialloop*i:\nend do: \nFactorialloop;" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}} {EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 0 "" }}}}{SECT 1 {PARA 3 "" 0 " " {TEXT 283 39 "Order of execution of commands in Maple" }{TEXT -1 1 " " }}{PARA 0 "" 0 "" {TEXT 265 347 "In a single execution group Maple \+ executes the commands in order from top to bottom. However, if you go \+ back and change some computation in a different execution group, then \+ it does not change the following results unless the entire worksheet i s executed once more. Here is a simple example. First execute the foll owing commands in the order given" }{TEXT -1 1 "." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 5 "x:=5;" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 5 "y:=3:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 7 "z:=x*y:" }}} {PARA 0 "" 0 "" {TEXT 265 189 "Now go back and change the 2 in the lin e x:=2 to a 5 and execute that command. Notice that the value of z has not changed. To change the value of z to 15 we must re-execute the l ine z:=x*y" }{TEXT -1 1 "." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 2 "z;" }}}{EXCHG }{EXCHG }{PARA 0 "" 0 "" {TEXT 265 226 "Of course, we w ould not have this problem if we put all the three commands in a singl e execution group as we do below. But after changing the 2 to a 5 in x :=2; we must execute the execution group for the changes to take effec t" }{TEXT -1 1 "." }}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 19 "x:=2:\n y:=3:\nz:=x*y:" }}}{EXCHG {PARA 0 "> " 0 "" {MPLTEXT 1 0 2 "z;" }}} {PARA 0 "" 0 "" {TEXT 313 175 "The moral of this is that when executin g the lines in a Maple worksheet it is important which order they are \+ executed in rather than the order in which they appear on the page" } {TEXT -1 0 "" }}}}{MARK "7 0 0" 33 }{VIEWOPTS 1 1 0 1 1 1803 1 1 1 1 } {PAGENUMBERS 0 1 2 33 1 1 }